package com.wm.task.dao;

import com.wm.task.util.DbHelper;
import com.wm.task.vo.User;

import java.sql.*;

public class MarketDAO {
    private User user;

    public boolean register(User user) {
        String sqlAdd = "INSERT INTO user (email,pass,name, time) VALUES (?,?,?,?);";//添加
        Connection connection = DbHelper.dbConn();
        try {
            PreparedStatement ps = connection.prepareStatement("SELECT * FROM user WHERE email = ?");
            ps.setString(1, user.getEmail());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("用户已存在");
                return false;
            }
        } catch (SQLException e) {
            //throw new RuntimeException(e);
        }
        DbHelper.upDate(sqlAdd, user.getEmail(), user.getPass(), user.getName(), user.getTime());
        return true;
    }

    public User login(String email, String pass) {
        Connection connection = DbHelper.dbConn();
        try {
            PreparedStatement ps = connection.prepareStatement("SELECT * FROM user WHERE email = ? and pass = ?");
            ps.setString(1, email);
            ps.setString(2, pass);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                user = new User(rs.getString("email"), rs.getString("name"), rs.getString("time"));

                return user;
            }
        } catch (SQLException e) {
            //throw new RuntimeException(e);
        }
        return user;


    }
    public void changePass(String pass, User user) {
        String sqlAdd = "UPDATE user SET pass = ? WHERE email = ?;";//修改
        DbHelper.upDate(sqlAdd, pass, user.getEmail());
    }
}
